package com.example.tianling.admin.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tianling.admin.service.CarouselService;
import com.example.tianling.admin.service.GoodService;
import com.example.tianling.admin.vo.CarouselVo;
import com.example.tianling.common.utils.MinioUtils;
import com.example.tianling.dao.CarouselMapper;
import com.example.tianling.dao.ProductMapper;
import com.example.tianling.entity.CarouselEntity;
import com.example.tianling.entity.ProductEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.example.tianling.common.utils.ImageUtils.compress;
import static com.example.tianling.common.utils.toImg.convert;
import static com.example.tianling.common.utils.toImg.getImageFormat;

@Service
public class CarouselSeviceImpl
        extends ServiceImpl<CarouselMapper, CarouselEntity>
        implements CarouselService {
    @Autowired
    private CarouselMapper carouselMapper;
    @Autowired
    private MinioUtils minioUtils;
    @Override
    public  Integer uploadCarousel(CarouselEntity carouselEntity){
        JSONObject res = new JSONObject();
        try {
            BufferedImage image = convert(carouselEntity.getImageUrl());
            BufferedImage compressedImage = compress(image); // Compress the image using your compress method
//            String originalFilename = file.getOriginalFilename(); // 这里替换成你的文件名
//            String fileExtension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            String fileExtension = getImageFormat(carouselEntity.getImageUrl());
            res = minioUtils.uploadBufferedImage(compressedImage, "rotation", fileExtension); // Upload the compressed image
        } catch (Exception e) {
            e.printStackTrace();
            res.put("code", 0);
            res.put("msg", "上传失败");
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss ");
        String date = formatter.format(new Date(System.currentTimeMillis()));
        carouselEntity.setCreatedTime(date);
        carouselEntity.setImageUrl(res.get("msg").toString());
        System.out.println(res.toJSONString());
        return carouselMapper.insert(carouselEntity);
    }


    public  Integer uodataCarousel(CarouselEntity carouselEntity){
       return carouselMapper.updateById(carouselEntity);

    }

    public  List<CarouselEntity> getAllCarousel(){
        QueryWrapper<CarouselEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("order_index");
        List<CarouselEntity> result = carouselMapper.selectList(queryWrapper);
//        List<CarouselVo> re = new ArrayList<>();
//        for(CarouselEntity r:result){
//            re.add(new CarouselVo(r.getId(),r.getTitle(),r.getImageUrl(),r.getOrderIndex()));
//        }
        return result;
    }

    public CarouselEntity getOneCarousel(String id){
        System.out.println(carouselMapper.selectById(id));
        return carouselMapper.selectById(id);
    }

    public Integer deleteCarousel(String id){
        return carouselMapper.deleteById(id);
    }
}
